package com.zxp.web.sys_menu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxp.web.constants.Constants;
import com.zxp.web.sys_menu.entity.AssignTreeVO;
import com.zxp.web.sys_menu.entity.AssignTreeParam;
import com.zxp.web.sys_menu.entity.SysMenu;
import com.zxp.web.sys_menu.mapper.SysMenuMapper;
import com.zxp.web.sys_menu.service.SysMenuService;
import com.zxp.web.sys_user.entity.SysUser;
import com.zxp.web.sys_user.service.SysUserService;
import com.zxp.web.utils.SystemUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author MasterZ
 * @Date 2025/10/4 09:42
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public List<SysMenu> getParent() {
        String[] types = {Constants.MENU_TYPE_MENU, Constants.MENU_TYPE_MENUITEM};
        List<String> strings = Arrays.asList(types);
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(SysMenu::getType, strings).orderByAsc(SysMenu::getOrderNum);
        List<SysMenu> list = this.list(queryWrapper);
        //获取根节点
        SysMenu root = SystemUtils.getRoot();
        list.add(root);
        return SystemUtils.toTree(list, Constants.MENU_ROOT_PARENT_ID);
    }

    @Override
    public List<SysMenu> getMenuByUserId(Long userId) {
        return this.baseMapper.getMenuByUserId(userId);
    }

    @Override
    public List<SysMenu> getMenuByRoleId(Long roleId) {
        return this.baseMapper.getMenuByRoleId(roleId);
    }

    @Override
    public AssignTreeVO getAssignTree(AssignTreeParam param) {
        //查询用户信息
        SysUser user = sysUserService.getById(param.getUserId());
        List<SysMenu> menuList;
        //判断是否超级管理员
        if (StringUtils.isNotEmpty(user.getIsAdmin()) && Constants.IS_ADMIN.equals(user.getIsAdmin())) {
            QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().orderByAsc(SysMenu::getOrderNum);
            //是超级管理员查询所有菜单
            menuList = this.list(queryWrapper);
        } else {
            menuList = this.getMenuByUserId(param.getUserId());
        }
        List<SysMenu> tree = SystemUtils.toTree(menuList, Constants.MENU_ROOT_ID);
        //查询角色原来的菜单
        List<SysMenu> roleList = this.getMenuByRoleId(param.getRoleId());
        List<Long> ids = new ArrayList<>();
        Optional.ofNullable(roleList).orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(role -> {
                    ids.add(role.getMenuId());
                });
        //组装返回的数据
        AssignTreeVO vo = new AssignTreeVO();
        vo.setCheckList(ids.toArray());
        vo.setMenuList(tree);
        return vo;
    }
}
